package com.example.fw;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.tests.GroupData;
import com.example.utils.SortedListOf;

public class GroupHelper extends HelperBase {
	
	private SortedListOf<GroupData> cachedGroups;
	
	public SortedListOf<GroupData> getGroups() {
		if (cachedGroups == null) {
			rebuildCach();
		}
		return cachedGroups;
		
	}
	
	private void rebuildCach() {
		cachedGroups = new SortedListOf<GroupData>();
		manager.navigateTo().groupPage();
		List<WebElement> checkboxes = driver.findElements(By.name("selected[]"));
		for (WebElement checkbox : checkboxes) {
			String title = checkbox.getAttribute("title");
			String groupname = title.substring("Select [".length(), title.length() - ")".length());
			cachedGroups.add(new GroupData().withgroupname(groupname));
		}
	}

	public GroupHelper createGroup(GroupData group) {
		manager.navigateTo().groupPage();
		initGroupCreation();
		fillGroupsForm(group);
		submitGroupCreation();
		returnToGroupsPage();
		rebuildCach();
		return this;
	}
	
	public GroupHelper modifyGroup(int index, GroupData group) {
		
		initGroupModification(index);
		fillGroupsForm(group);
		submitGroupModification();
		returnToGroupsPage();
		rebuildCach();
		return this;
		
	}
	
	public GroupHelper deleteGroup(int index) {
		selectGroupByIndex(index);
		submitGroupDeletion();
		returnToGroupsPage();
		rebuildCach();
		return this;
	}

//-----------------------------------------------------------------------------
	
	public GroupHelper(ApplicationManager manager) {
		super(manager);
	}

	public GroupHelper initGroupCreation() {
		click(By.name("new"));
		return this;
	}

	public GroupHelper fillGroupsForm(GroupData group) {
		type(By.name("group_name"), group.getGroupname());
		type(By.name("group_header"), group.getHeader());
		type(By.name("group_footer"), group.getFooter());	
		return this;
	}

	public GroupHelper returnToGroupsPage() {
		click(By.linkText("group page"));
		return this;
	}

	public GroupHelper submitGroupCreation() {
		click(By.name("submit"));
		cachedGroups = null; 
		return this;
	}


	private GroupHelper selectGroupByIndex(int index) {
		click(By.xpath("//input[@name='selected[]']["+ (index+1) + "]"));
		return this;
	}

	public GroupHelper initGroupModification(int index) {
		selectGroupByIndex(index);
		click(By.name("edit"));
		return this;
	}

	public GroupHelper submitGroupModification() {
		click(By.name("update"));
		cachedGroups = null; 
		return this;
	}

	private GroupHelper submitGroupDeletion() {
		click(By.name("delete"));
		cachedGroups = null; 
		return this;
	}


}
